<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('geofence')">返回</el-button>
  </div>
  <div id="mapContainer" style="width: 100%;height: 84vh;">
  </div>
  <el-dialog title="围栏信息" v-model="dialogVisible">
    <el-form>
      <el-form-item label="围栏名称">
        <el-input v-model="genofence.name" placeholder="请输入围栏名称"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="savaGeofence" >确定</el-button>
    </template>
  </el-dialog>
</template>



<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import { DrawScene, DrawControl ,OperateEventType} from 'bmap-draw';
import {ElMessage} from "element-plus";
import qs from "qs";
import axios from "axios";


//定义对象保存电子围栏给后端发送的数据
const genofence=ref({
  name:'',//围栏名字
  position:{}//围栏位置数据
})
var map;
onMounted(()=>{
  nextTick(()=>{
     map=new BMapGL.Map("mapContainer");
    let point = new BMapGL.Point(116.404, 39.915);
    //let point = new BMapGL.Point(121.750676, 36.871378);
    map.centerAndZoom(point, 15);
    // 添加鼠标绘制控件
    //创建绘制场景类,这是使用其他绘制功能的前提,需要DrawScene导入
    const scene = new DrawScene(map);
    //创建绘制控件类,并将上方的绘制场景类作为构造参数传入,注意:DrawControl需要导入
    const drawContrl = new DrawControl(scene, {
      //对控件进行配置
      //关闭文字的绘制提示
      enableTips: true,
      //绘制控件的定位位置
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      //设置绘制项的形状,circle:圆形,rectangle:矩形
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    scene.addEventListener(OperateEventType.COMPLETE, (event) => {
      console.log(event);
      dialogVisible.value=true;
      //获取出发完成围栏的数据信息
     let overlay= event.target.overlay;
     if (overlay instanceof BMapGL.Circle){//圆星
       //设置名字为圆形
        genofence.value.position.type='circle';
        //设置圆形半径值
        genofence.value.position.radius=overlay.getRadius();
        //设则圆形的经纬度
        genofence.value.position.longitude=overlay.getCenter().lng;
        genofence.value.position.latitude=overlay.getCenter().lat;
        console.log(genofence.value);
     }else {
       let recPoints=[];
       //获取四个角的坐标值
       for (let i=0;i<4;i++){
         recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
       }
       //将四个角的坐标值以逗号分隔,利用join方法拼接成字符串
       genofence.value.position.recPoints=recPoints.join(',');
       //设置名字为矩形
       genofence.value.position.type='rectangle';
       console.log(genofence.value);
     }
})
    //将绘制控件添加到地图中
    map.addControl(drawContrl);
  })
})
//开关
const dialogVisible=ref(false);
//取消按钮
const handleClose=()=>{
  if(confirm('您确认要关闭本弹窗吗?')){
    genofence.value={name:'',position:{}};
    dialogVisible.value=false;
    map.clearOverlays();
  }
}
const savaGeofence=()=>{
  if(!genofence.value.name||genofence.value.name.trim()==''){
    ElMessage.error('围栏名称不能为空');
    return;
  }
  genofence.value.position=qs.stringify(genofence.value.position);
  let data=qs.stringify(genofence.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/geofence/save',data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("新增成功");
      dialogVisible.value=false;
      router.push('geofence');
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
</script>


<style scoped>

</style>